<template>
  <div class="wrapper">
    <div class="title">我的订单</div>
    <div class="content">
      <div class="item" v-for="(item, index) in list" :key="index">
        <div class="item__header">
          <span class="item__header__title">{{ item.shopName }}</span>
          <span class="item__header__change">{{
            item.isCanceled ? "已取消" : "支付成功"
          }}</span>
        </div>
        <div class="product">
          <div class="product__imgs">
            <template
              v-for="(productItem, productIndex) in item.products"
              :key="productIndex"
            >
              <img
                :src="productItem.product.imgUrl"
                class="product__imgs__img"
                v-if="productIndex <= 3"
              />
            </template>
          </div>
          <div class="product__info">
            <div class="product__info__price">
              &yen;{{ item.products.price }}
            </div>
            <div class="product__info__count">
              共{{ item.products.total }}件
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <Docker :num="2" />
</template>

<script>
import Docker from "../../components/docker/Docker.vue";
import { get } from "../../utils/request";
import { reactive, toRefs } from "vue";
export default {
  name: "orderList",
  components: { Docker },
  setup() {
    const data = reactive({ list: [] });
    const getOrderList = async () => {
      try {
        const result = await get("api/order");
        if (result.data.errno === 0) {
          data.list = result.data.data;
          data.list.forEach((item, index) => {
            let price = 0;
            let total = 0;
            const products = item.products;
            products.forEach((product) => {
              price += product.orderSales * product.product.price;
              total += product.orderSales;
            });
            data.list[index].products.price = price.toFixed(2);
            data.list[index].products.total = total;
          });
        }
      } catch (e) {
        alert("请求失败");
      }
    };
    getOrderList();
    const { list } = toRefs(data);
    return { list };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
.wrapper {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0.5rem;
  overflow-y: auto;
  background-color: $color-f8f8;
}
.title {
  background-color: $color-fff;
  height: 0.44rem;
  text-align: center;
  line-height: 0.44rem;
  font-size: 0.16rem;
  color: $content-fontcolor;
}
.item {
  margin: 0.16rem 0.18rem 0;
  background-color: $color-fff;
  padding: 0.16rem;
  border-radius: 0.04rem;
  &__header {
    display: flex;
    justify-content: space-between;
    &__title {
      color: $content-fontcolor;
      font-size: 0.16rem;
      font-weight: 600;
      line-height: 0.22rem;
    }
    &__change {
      color: $color-999;
      font-size: 0.14rem;
      line-height: 0.2rem;
    }
  }
}
.product {
  display: flex;
  margin-top: 0.16rem;
  &__imgs {
    flex: 1;
    &__img {
      width: 0.4rem;
      height: 0.4rem;
      margin-right: 0.12rem;
    }
  }
  &__info {
    &__price {
      color: $color-e93b;
      font-size: 0.14rem;
      line-height: 0.2rem;
    }
    &__count {
      font-size: 0.12rem;
      color: $content-fontcolor;
      margin-top: 0.04rem;
      float: right;
    }
  }
}
</style>